import logging

from motor.motor_asyncio import AsyncIOMotorClient


class MongoDBClient:
    dbjsonhtml: AsyncIOMotorClient = None  # 集群连接对象
    dbjsonlatest: AsyncIOMotorClient = None  # 集群连接对象


class DataBase:
    client: AsyncIOMotorClient = None
    htmljson = None


async def connect_to_mongo(mongodb_conn_str: str, db_name: str):
    logging.info("connecting to mongo...")
    db = DataBase()
    db.client = AsyncIOMotorClient(mongodb_conn_str,
                                   maxPoolSize=10,
                                   minPoolSize=10)
    # get a collection
    # Format db.<database_name>.<collection_name>
    setattr(DataBase, db_name, db.client[db_name])
    logging.info("connected to {}".format(db_name))
    return db


async def close_mongo_connection(db):
    logging.info("closing connection...")
    db.client.close()
    logging.info("closed connection")


class Coll(object):

    @classmethod
    def get_table_conn(cls, table):
        return eval("Coll.get_{}".format(table))()

    @classmethod
    def get_step_info(cls):
        return MongoDBClient.dbjsonhtml.client.htmljson.step_info

    @classmethod
    def get_wanfang_ref(cls):
        return MongoDBClient.dbjsonhtml.client.htmljson.wanfang_ref

    @classmethod
    def get_paper(cls):
        return MongoDBClient.dbjsonhtml.client.htmljson.paper

    # 用作错误捕获测试表1
    @classmethod
    def get_errs(cls):
        return MongoDBClient.dbjsonhtml.client.htmljson.err_test

    # 用作错误捕获测试表2
    @classmethod
    def get_errs_part2(cls):
        return MongoDBClient.dbjsonhtml.client.htmljson.justtest


    @classmethod
    def get_paper_latest(cls):
        return MongoDBClient.dbjsonlatest.client.htmljsonlatest.a_paper_latest

    @classmethod
    def get_other(cls):
        return MongoDBClient.dbjsonhtml.client.htmljson.other

    @classmethod
    def get_cnki_article_detail_new(cls):
        return MongoDBClient.dbjsonhtml.client.htmljson.cnki_article_detail_new

    @classmethod
    def get_except_client(cls):
        return MongoDBClient.dbjsonhtml.client.htmljson.client_except

    @classmethod
    def get_request_api_fail(cls):
        return MongoDBClient.dbjsonhtml.client.htmljson.client_api_fail

    @classmethod
    def get_post_user_err(cls):
        return MongoDBClient.dbjsonhtml.client.htmljson.client_user_err

    @classmethod
    def get_server_exception(cls):
        return MongoDBClient.dbjsonhtml.client.htmljson.server_exception

    @classmethod
    def get_ftp_patent_raw(cls):
        return MongoDBClient.dbjsonhtml.client.htmljson.ftp_patent_raw

    @classmethod
    def get_ftp_patent_his(cls):
        return MongoDBClient.dbjsonhtml.client.htmljson.ftp_patent_his


    @classmethod
    def get_journal_article(cls):
        return MongoDBClient.dbjsonhtml.client.htmljson.journal_article